package systems;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import helpers.CacheHelper;

public class Recently {
	
	private static String prefix = "recently_";
	
	public static void insert(String name, String type) {
		int limit = 500;
		
		String cacheName = prefix + "_type_" + type;
		
		List<String> domains = (List<String>) CacheHelper.get(cacheName);
		
		if (domains == null) {
			domains = new ArrayList<String>();
		}
		
		if (domains.contains(name)) {
			domains.remove(name);
		}
		
		if (domains.size() >= limit) {
			domains.remove(limit - 1);
		}
		
		domains.add(0, name);
		
		CacheHelper.put(cacheName, domains);
	}
	
	public static void remove(String name, String type) {
		
		String cacheName = prefix + "_type_" + type;
		
		boolean isChange = false;
		
		List<String> domains = (List<String>) CacheHelper.get(cacheName);
		
		if (domains == null) {
			domains = new ArrayList<String>();
		}
		
		if (domains.contains(name)) {
			domains.remove(name);
			isChange = true;
		}
		
		if (isChange) {
			CacheHelper.put(cacheName, domains);
		}
	}
	
	public static List<String> getAll(String type) {
		String cacheName = prefix + "_type_" + type;
		
		List<String> domains = (List<String>) CacheHelper.get(cacheName);
		
		if (domains == null) {
			domains = new ArrayList<String>();
		}
		
		return domains;
	}
}
